package com.me.jingdongshop.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.me.common.base.BaseActivity;
import com.me.common.base.Global;
import com.me.common.base.OttoBus;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.SearchRecommend;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.presenter.TestFragmentPre;
import com.me.jingdongshop.ui.adapter.LastSearchAdapter;
import com.me.jingdongshop.ui.adapter.SearchPagerAdapter;
import com.squareup.otto.Subscribe;

import java.util.ArrayList;
import java.util.List;

import static com.me.jingdongshop.R.id.iv_searching_point;

/**
 * Created by CJC on 2017/5/8.
 */

public class HomeSearchPagerActivity extends BaseActivity {

    private ImageView ivSearchBack;
    private LinearLayout llSearchSelect;
    private TextView tvSearchProduct;
    private AutoCompleteTextView actvSearch;
    private ImageView ivSearchingPoint;
    private GridView gvHotSearch;
    private GridView gvLastSearch;
    private LinearLayout llCleanSearch;

    private List<String> hotSearchList;
    private List<String> lastSearchList = new ArrayList<>();
    private LastSearchAdapter mLastSearchAdapter;


    @Override
    public int getLayoutRes() {
        return R.layout.search_pager;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        OttoBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OttoBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onEvent(String bean) {
        actvSearch.setText(bean);


    }

    @Override
    public void initView() {
        ivSearchBack = (ImageView) findViewById(R.id.iv_search_back);
        ivSearchBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        llSearchSelect = (LinearLayout) findViewById(R.id.ll_search_select);
        tvSearchProduct = (TextView) findViewById(R.id.tv_search_product);
        actvSearch = (AutoCompleteTextView) findViewById(R.id.actv_search);
        ivSearchingPoint = (ImageView) findViewById(iv_searching_point);
        gvHotSearch = (GridView) findViewById(R.id.gv_hot_search);
        gvLastSearch = (GridView) findViewById(R.id.gv_last_search);
        llCleanSearch = (LinearLayout) findViewById(R.id.ll_clean_search);
    }

    private String keyword;

    @Override
    public void initListener() {
        ivSearchingPoint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                keyword = actvSearch.getText().toString();
                Intent intent = new Intent(Global.mContext, ProductlistActivity.class);
                intent.putExtra("keyword", keyword);
                startActivity(intent);
                if (lastSearchList.size() >= 20) {
                      lastSearchList.remove(0);
                    mLastSearchAdapter.notifyDataSetChanged();
                } else {
                    if (keyword.isEmpty()){
                        return;
                    }else if (lastSearchList.contains(keyword)){
                        return;
                    }else {
                        lastSearchList.add(keyword);
                        mLastSearchAdapter.notifyDataSetChanged();
                    }


                }
            }
        });
    }

    @Override
    public void initData() {
        TestFragmentPre testFragmentPre = new TestFragmentPre(this);
        testFragmentPre.getSearchRecData();

        actvSearch.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
                R.layout.auto_complete_textview_item, R.id.item_txt, new String[]{"奶粉", "女裙", "外套", "长裤", "T恤"}));
        mLastSearchAdapter =new LastSearchAdapter(this, lastSearchList);
                gvLastSearch.setAdapter(mLastSearchAdapter);

    }

    @Override
    public void onClick(View v, int id) {

    }

    //请求网络成功
    @Override
    public void onHttpSuccess(int reqType, Message msg) {
        if (reqType == IHttpService.HTTP_SEARCH_REC) {
            SearchRecommend bean = (SearchRecommend) msg.obj;
            hotSearchList = bean.getSearchKeywords();

            gvHotSearch.setAdapter(new SearchPagerAdapter(this, hotSearchList));
        }

    }

    @Override
    public void onHttpError(int reqType, String error) {

    }
}
